package com.kczd.jinlan.view.mine;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.kczd.jinlan.R;
import com.kczd.jinlan.utils.Config;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;

import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

/**
 * yang_g
 * 钱包
 * Created by Administrator on 2017/5/16.
 */

public class WalletActivity extends Activity implements View.OnClickListener {
    private static final String TAG = "WalletActivity.class";
    String url = Config.net_addr + "user/account";

    private TextView text_bank;//银行卡
    private TextView id_fanhui;//返回
    private TextView text_small;//零钱明细
    private Button but_recharge;//充值
    private TextView mYueE;//余额
    private ImageView image_alipay;//支付宝
    private ImageView image_weixin;//微信
    private ImageView image_bank;//银行卡
    private int a = 1;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.wallet_activity);

//        获取账户余额
        new Thread(new Runnable() {
            @Override
            public void run() {
                getMoney();
            }
        }).start();

    }

    public void getMoney() {
        //创建okHttpClient对象
        OkHttpClient client = new OkHttpClient();
        RequestBody requestBody = new FormBody.Builder()
                .add("token", Config.user.getToken())
                .build();
        Request request = new Request.Builder()
                .url(url)
                .post(requestBody)
                .build();
        try {
            Response response = client.newCall(request).execute();
            String strResult = response.body().string();
            Message msg = new Message();
            Bundle data = new Bundle();
            data.putString("ret_json", strResult);
            msg.setData(data);
            handler.sendMessage(msg);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            Bundle data = msg.getData();
            String val = data.getString("ret_json");
            Log.e(TAG, val);
            val = val.substring(val.indexOf("{"), val.lastIndexOf("}") + 1);
            JSONObject jsonResult;
            try {
                jsonResult = new JSONObject(val);
                // UI界面的更新等相关操作
                if (Config.code.equals(jsonResult.getString("code"))) {
                    JSONObject jb = jsonResult.getJSONObject("data");
                    JSONObject jbuser = jb.getJSONObject("user");
                    String money = jbuser.getString("user_money");
                    mYueE.setText(money);

                }
            } catch (JSONException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    };

    @Override
    public void onContentChanged() {
        super.onContentChanged();


        text_small = (TextView) findViewById(R.id.text_small);
        text_small.setOnClickListener(this);
        but_recharge = (Button) findViewById(R.id.but_recharge);
        but_recharge.setOnClickListener(this);
        id_fanhui = (TextView) findViewById(R.id.id_fanhui);
        id_fanhui.setOnClickListener(this);
        mYueE = (TextView) findViewById(R.id.id_wallet_yue);

        image_alipay = (ImageView) findViewById(R.id.image_alipay);
        image_alipay.setOnClickListener(this);

        image_weixin = (ImageView) findViewById(R.id.image_weixin);
        image_weixin.setOnClickListener(this);

        image_bank = (ImageView) findViewById(R.id.image_bank);
        image_bank.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.text_small://零钱明细
                startActivity(new Intent(WalletActivity.this, SmallActivity.class));
                break;
            case R.id.but_recharge://充值
                if (a == 1) {
                    Log.e("a1", "支付宝");

                }
                if (a == 2) {
                    Log.e("a2", "微信");

                }
                if (a == 3) {
                    Log.e("a3", "银行卡");
                    startActivity(new Intent(WalletActivity.this, RechargeActivity.class));
                }
                break;
            case R.id.id_fanhui://返回
                finish();
                break;

            case R.id.image_alipay://支付宝
                a = 1;
                image_alipay.setImageResource(R.mipmap.xuanzhong);
                image_weixin.setImageResource(R.mipmap.notxuanzhong);
                image_bank.setImageResource(R.mipmap.notxuanzhong);
                break;

            case R.id.image_weixin://微信
                a = 2;
                image_alipay.setImageResource(R.mipmap.notxuanzhong);
                image_weixin.setImageResource(R.mipmap.xuanzhong);
                image_bank.setImageResource(R.mipmap.notxuanzhong);
                break;

            case R.id.image_bank://银行卡
                a = 3;
                image_alipay.setImageResource(R.mipmap.notxuanzhong);
                image_weixin.setImageResource(R.mipmap.notxuanzhong);
                image_bank.setImageResource(R.mipmap.xuanzhong);
                break;
        }
    }
}
